<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/10/23
 * Time: 16:19
 */

namespace app\adminapiv3\service;


use app\common\service\Host;
use app\common\service\HostServiceHistory;

class HostService extends Base
{
    /**
     * @name 服务商列表
     * @param $params
     * @param int $limit
     * @return array
     * @author wx
     * @date 2019/10/24 10:15
     */
    function list($params, $limit = 10)
    {
        //1. 判断合同是否过期 是否服务中
        $this->checkTimeOut();

        //2. 查询服务商数据
        $server = new \app\common\service\HostService();
        $list = $server->list($params, $limit);

        //3. 查询服务商类型
        $serviceType = new \app\common\service\HostServiceTypeRelation();
        foreach ($list['data'] as $k => $v){
            $list['data'][$k]['type'] = $serviceType->findType($v['id']);
        }

        return $list;
    }

    /**
     * @name 判断合同是否过期 是否服务中
     * @author wx
     * @date 2019/10/24 16:26
     */
    function checkTimeOut()
    {
        $server = new \app\common\service\HostService();

        $data = $server->findAll('id, start_time, end_time, status');

        $nowTime = time();
        foreach ($data as $k => $v){
            //结束时间 合作中
            if ($v['status'] == 1 && $v['end_time'] != 0 && $nowTime > $v['end_time'] ) {
                //合同自然过期,改变服务商状态
                $this->endContract($v['id'], 3, '合同自然过期');
            }

            //修改服务中
            if($v['status'] == 1 && $v['start_time'] > $nowTime){
                $server->modify(['id' => $v['id']], ['status' => 3]);
            }
            if($v['status'] == 3 && $v['start_time'] < $nowTime){
                $server->modify(['id' => $v['id']], ['status' => 1]);
            }
        }
    }

    /**
     * @name 判断是否存在
     * @author wx
     * @date 2019/10/24 16:37
     */
    function checkExist($condition)
    {
        $server = new \app\common\service\HostService();
        $info = $server->findData($condition);

        if($info){
            return true;
        }else{
            return false;
        }
    }
    /**
     * @name 添加
     * @param array $addData
     * @param $type
     * @return bool
     * @author wx
     * @date 2019/10/24 10:53
     */
    function add($addData = array(), $type)
    {
        //1. 验证服务商是否重复
        if ($this->checkExist(['name' => $addData['name']])) {
            $this->setError($this->CODE_FAIL,'请勿添加相同服务商');
            return false;
        }

        //2. 添加服务商数据
        $addData['status'] = 1;
        $addData['start_time'] = strtotime($addData['start_time']);
        if($addData['start_time'] > time()){
            //未开始服务
            $addData['status'] = 3;
        }
        $addData['end_time'] = 0;
        $addData['ctime'] = time();

        $serviceServer = new \app\common\service\HostService();
        $serviceId = $serviceServer->add($addData);

        //3. 添加服务商与服务商类型的关系
        $serviceRelationServer = new \app\common\service\HostServiceTypeRelation();

        $insertAllData = array();
        foreach ($type as $k => $v){
            $insertAllData[$k]['service_id'] = $serviceId;
            $insertAllData[$k]['type_id'] = $v;
        }

        $serviceRelationServer->addAll($insertAllData);


        //4. 添加服务历史
        $serviceHistoryServer = new HostServiceHistory();
        $addHistoryData['service_id'] = $serviceId;//服务商id
        $addHistoryData['start_time'] = strtotime($addData['start_time']);//签约时间
        $addHistoryData['content'] = empty($addData['content']) ? '首次添加服务商' : $addData['content'];
        $addHistoryData['end_time'] = 0;
        $addHistoryData['option_time'] = time();

        $serviceHistoryServer->add($addHistoryData);

        return true;
    }

    /**
     * @name 详情
     * @param $serviceId
     * @return array|bool|null|\PDOStatement|string|\think\Model
     * @author wx
     * @date 2019/10/24 11:03
     */
    function detail($serviceId)
    {
        //1. 查询服务商数据
        $server = new \app\common\service\HostService();
        $info = $server->findData(['id' => $serviceId]);

        if(!$info){
            $this->setError($this->CODE_FAIL,'数据错误');
            return false;
        }

        //2. 查询服务商类型
        $serviceTypeServer = new \app\common\service\HostServiceTypeRelation();
        $info['type'] = $serviceTypeServer->findTypeId($serviceId);

        //时间处理
        $info['end_reason'] = $info['end_reason'] == 0 ? null : $info['end_reason'];
        $info['start_time'] = date('Y-m-d', $info['start_time']);
        if ($info['end_time']) {
            $info['end_time'] = date('Y-m-d', $info['end_time']);
        }
        unset($info['ctime']);

        return $info;
    }

    /**
     * @name 修改
     * @param $serviceId
     * @param $updateData
     * @param $type array
     * @return bool
     * @author wx
     * @date 2019/10/24 13:35
     */
    function modify($serviceId, $updateData, $type)
    {
        //1.修改服务商数据
        $updateData['start_time'] = strtotime($updateData['start_time']);

        if ($updateData['start_time'] > time()) {
            $updateData['status'] = 3;
        }

        $service = new \app\common\service\HostService();

        $service->modify(['id' => $serviceId], $updateData);

        //2.修改服务商类型
        $serviceTypeServer = new \app\common\service\HostServiceTypeRelation();
        $serviceTypeServer->del(['service_id' => $serviceId]);

        foreach ($type as $k => $v){
            $insertAllData[$k]['service_id'] = $serviceId;
            $insertAllData[$k]['type_id'] = $v;
        }

        $serviceTypeServer->addAll($insertAllData);

        return true;
    }

    /**
     * @name 删除
     * @param $serviceId
     * @return bool
     * @author wx
     * @date 2019/10/24 13:57
     */
    function del($serviceId)
    {
        //1. 删除服务商数据
        $service = new \app\common\service\HostService();
        $condition[] = array('id', 'in', $serviceId);
        $service->del($condition);

        //2. 删除关系
        $serviceTypeServer = new \app\common\service\HostServiceTypeRelation();
        $typeCondition[] = array('service_id', 'in', $serviceId);
        $serviceTypeServer->del($typeCondition);

        //3. 删除服务历史
        $serviceHistoryServer = new HostServiceHistory();
        $historyCondition[] = array('service_id', 'in', $serviceId);
        $serviceHistoryServer->del($historyCondition);

        //4. 重置与主机关系
        $hostServer = new Host();
        $hostSafeCondition['safe_provider_id'] = $serviceId;
        $hostMaintainCondition['maintain_provider_id'] = $serviceId;
        $hostDevelopCondition['develop_provider_id'] = $serviceId;
        $hostServer->modify($hostSafeCondition, ['safe_provider_id' => 0]);
        $hostServer->modify($hostMaintainCondition, ['maintain_provider_id' => 0]);
        $hostServer->modify($hostDevelopCondition, ['develop_provider_id' => 0]);

        return true;
    }

    /**
     * @name 服务历史
     * @param $serviceId
     * @param int $limit
     * @return array
     * @author wx
     * @date 2019/10/24 14:14
     */
    function serviceHistory($serviceId, $limit = 10)
    {
        $serviceHistoryServer = new HostServiceHistory();
        $list = $serviceHistoryServer->list($serviceId, $limit);

        foreach ($list['data'] as $k => $v){
            if($v['start_time']){
                $list['data'][$k]['start_time'] = date('Y-m-d',$v['start_time']);
            }else{
                $list['data'][$k]['start_time'] = '-';
            }

            if($v['end_time']){
                $list['data'][$k]['end_time'] = date('Y-m-d',$v['end_time']);
            }else{
                $list['data'][$k]['end_time'] = '-';
            }

            if($v['option_time']){
                $list['data'][$k]['option_time'] = date('Y-m-d',$v['option_time']);
            }else{
                $list['data'][$k]['option_time'] = '-';
            }
        }

        return $list;
    }

    /**
     * @name 续约更新
     * @param $serviceId
     * @param $endTime
     * @param $content
     * @return bool
     * @author wx
     * @date 2019/10/24 14:39
     */
    function updateService($serviceId, $endTime, $content)
    {
        //1.查询服务商数据
        $server = new \app\common\service\HostService();

        //确定服务开始时间
        $startTime = time();
        $endTime = strtotime($endTime);
        $status = 1;

        //2.修改服务商数据表
        $updateData = array(
            'start_time' => $startTime,
            'end_time' => $endTime,
            'content' => $content,
            'status' => $status
        );
        $server->modify(['id' => $serviceId], $updateData);


        //3.加入服务历史数据
        $serviceHistoryServer = new HostServiceHistory();
        $addHistory = array(
            'service_id' => $serviceId,
            'start_time' => $startTime,
            'end_time' => $endTime,
            'content' => '续约更新:'.$content,
            'option_time' => time()
        );
        $serviceHistoryServer->add($addHistory);

        return true;
    }

    function endContract($serviceId, $endReason, $endReasonDescription)
    {
        $server = new \app\common\service\HostService();
        if($endReason == 3){
            $endReasonDescription = empty($endReasonDescription) ? '' : $endReasonDescription;
            $content = $endReasonDescription;
        }elseif($endReason == 1){
            $content = '该公司主动放弃';
            $endReasonDescription = '';
        }else{
            $content = '该公司服务质量不佳';
        }

        //1.查询服务商数据
        $info = $server->findData(['id' => $serviceId, 'status' => 1]);
        if(!$info){
            $this->setError($this->CODE_FAIL, '未查询到数据');
            return false;
        }
        $startTime = $info['start_time'];

        //2.修改服务商的数据
        $updateData = array(
            'status' => 2,
            'end_time' => time(),
            'end_reason' => $endReason,
            'end_reason_description' => $endReasonDescription
        );
        $server->modify(['id' => $serviceId], $updateData);

        //3.添加服务商服务历史数据
        $serviceHistoryServer = new HostServiceHistory();


        $historyAdd = array(
            'service_id' => $serviceId,
            'end_time' => time(),
            'start_time' => $startTime,
            'content' => '合同终止:'.$content,
            'option_time' => time(),
        );
        $serviceHistoryServer->add($historyAdd);

        return true;
    }
}